#include "stdafx.h"
#include "TestIStorage.h"

TestIStorage::TestIStorage()
{
}

TestIStorage::~TestIStorage()
{
}

bool TestIStorage::save(CString path)
{
	try
	{
		IStorage*stg = NULL;
		HRESULT hr = ::StgCreateDocfile(_bstr_t(path),
			STGM_WRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE, 0, &stg);
		_com_error err(hr);
		if (hr == S_OK)
		{
			save_extent(stg);

			stg->Release();
			return true;
		}
	}
	catch (CMemoryException* e)
	{
		e->ReportError();
	}
	catch (CFileException* e)
	{
		e->ReportError();
	}
	catch (CException* e)
	{
		e->ReportError();
	}
	catch (...)
	{
		ASSERT(0);
	}

	return false;
}

bool TestIStorage::load(CString path)
{
	IStorage*stg = NULL;
	BSTR menu_file = path.AllocSysString();
	bool rt = false;
	if (S_OK == StgOpenStorage(menu_file, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, NULL, 0, &stg))
	{
		if (!load_extent(stg))
		{
			return false;
		}

		stg->Release();
	}
	return rt;
}

void TestIStorage::getDataToSave(dataToSave& bbox)
{
	bbox.m_ids.push_back(1);
	bbox.m_ids.push_back(3);
	bbox.m_ids.push_back(5);

	for (int i = 0; i < 10; i++)
	{
		CString cs;
		cs.Format(_T("cc %d"), i + 2);
		bbox.m_mapStringList.insert(i, cs);
	}

	CString csDa;
	bbox.m_mapStringList.getAt(0, csDa);

	bbox.pp.x = 100;
	bbox.pp.y = 120;

	bbox.x = 333;
	bbox.y = 334;
}

void TestIStorage::save_extent(IStorage*stg)
{
	ASSERT(stg);

	dataToSave bbox;
	getDataToSave(bbox);

	IStream *stm = NULL;
	if (S_OK == stg->CreateStream(_bstr_t(_T("extent")), STGM_SHARE_EXCLUSIVE | STGM_WRITE | STGM_CREATE, 0, 0, &stm))
	{
		ULONG nn;
		stm->Write(&bbox, sizeof(bbox), &nn);
		stm->Release();
	}
}

bool TestIStorage::load_extent(IStorage*stgParent)
{
	IStream *stm = NULL;
	if (S_OK == stgParent->OpenStream(L"extent", NULL, STGM_SHARE_EXCLUSIVE | STGM_READ, 0, &stm))
	{
		dataToSave bbox;
		ULONG nn;
		stm->Read(&bbox, sizeof(bbox), &nn);

		CString cs;

		int len = 10;
		for (int i = 0; i < len; i++)
		{
			bbox.m_mapStringList.getAt(i+1, cs);

			int kkk = 0;
		}
		stm->Release();
		return true;
	}
	return false;
}
